package com.netease.connectionpool.thread;

import com.netease.connectionpool.mypool.MyPoolFactory;
import com.netease.connectionpool.mypool.MyPooledConnection;

import java.sql.ResultSet;
import java.sql.SQLException;


public class QueryThread implements Runnable {

    public void run() {
        for (int i = 0; i < 1000; i++) {
            // 从连接池中申请获取一个连接
            MyPooledConnection myPooledConnection = MyPoolFactory.getInstance().getMyPooledConnection();

            try {
                ResultSet query = myPooledConnection.query("SELECT * FROM student");
                while (query.next()) {
                    String username = query.getString("name");
                    int age = query.getInt("age");
                    String sex = query.getString("sex");
                    System.out.println("Thread name:" + Thread.currentThread().getName() + ",name=" + username + ",age=" + age + ",sex=" + sex + ",使用管道=" + myPooledConnection.getConnection());
                }

                // 查询结束后调用方需要主动释放连接
                myPooledConnection.close();
                System.out.println("调用结束");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
